﻿var TopLayoutSettings = {
    defaults: {
        size: "auto"
		, minSize: 5
        , resizable: false
		, paneClass: "ui-layout-pane"
		, resizerClass: "ui-layout-resizer"
		, togglerClass: "ui-layout-toggler"
		, buttonClass: "ui-layout-button"
		, contentSelector: ".content"	// inner div to auto-size so only it scrolls, not the entire pane!
		, contentIgnoreSelector: "span"		// 'paneSelector' for content to 'ignore' when measuring room for content
		, togglerLength_open: 35			// WIDTH of toggler on north/south edges - HEIGHT on east/west edges
		, togglerLength_closed: 35			// "100%" OR -1 = full height
        , spacing_open: 0
        , spacing_closed: 0
		, hideTogglerOnSlide: true		// hide the toggler when pane is 'slid open'
		, togglerTip_open: "关闭面板"
		, togglerTip_closed: "打开面板"
		, resizerTip: "调节面板尺寸"
		, slidable: false
        //	effect defaults - overridden on some panes
		, fxName: "slide"		// none, slide, drop, scale
		, fxSpeed_open: 750
		, fxSpeed_close: 1500
		, fxSettings_open: { easing: "easeInQuint" }
		, fxSettings_close: { easing: "easeOutQuint" }
    }
    , west: {
        size: 250
        , spacing_open: 5
        , spacing_closed: 5
    }
    , center: {

}
};

function ajax(method, param, success) {
    $.ajax({
        url: "../dispatchsystem/proxy.aspx?m=" + method
        , dataType: "json"
        , type: "post"
        , data: param
        , success: function (data) {
            if (!AjaxError(data)) {
                success(data);
            }
        }
    });
};

function init() {
};

$(function () {

    if (!IsOnLine()) {
        return;
    }
    $('body').layout(TopLayoutSettings);

    ajax("getInitLngLat", {}, function (obj) {
        parkMSys.TryFunc("initMap","mapC", obj.lng, obj.lat, obj.level);
    });

    ajax("getCityRegions", {}, function (arr) {
        
        $("#dvRegions").append("#regionsTmpl", arr);
    });

});

function getAllInspectionManOfRegion(regionId, regionName) {
    $("#txtKey").val("");
    $("#dvDistrictListTitle").show().find("span.key").text(regionName);
    ajax("getAllInspectionManOfRegion", { id: regionId }, function (arr) {
        $("#dvDistrictList").empty().append("#manListTmpl", arr);
        $("#dvDistrictListTitle").find("span.sum").text(arr.length);
        map.clearOverlays();
        var pArr = [];
        $.each(arr, function (i, v) {
            var p = new BMap.Point(parseFloat(v.currentLng), parseFloat(v.currentLat));
            pArr.push(p);
            var img = parkMSys.jsSiteHost + "/images/man.jpg";
            var myIcon = new BMap.Icon(img, new BMap.Size(12, 29),
                                      {
                                          offset: new BMap.Size(6, 29),
                                          imageOffset: new BMap.Size(0, 0)
                                      });
            var marker = new BMap.Marker(p, { icon: myIcon });
            map.addOverlay(marker);

            marker.addEventListener("click", function () {
                var html = $("#manInfoTmpl").tmpl(v);
                var opts = {
                    width: 320,
                    height: 300,
                    offset: new BMap.Size(0, 0),
                    title: v.title
                }
                var infoWindow = new BMap.InfoWindow(html[0].outerHTML, opts);
                marker.openInfoWindow(infoWindow);
            });
        });
        map.setViewport(pArr);
    });
    ajax("getDistrictOfRegion", { id: regionId }, function (arr) {
        $("#dvDlst").show();
        $("#dvDistrict").empty().append("#tmplDistrict", arr);
    });
};

function getAllInspectionManOfDistrict(id, name) {
    $("#txtKey").val("");
    $("#dvDistrictListTitle").show().find("span.key").text(name);
    ajax("getAllInspectionManOfDistrict", { id: id }, function (arr) {
        $("#dvDistrictList").empty().append("#manListTmpl", arr);
        $("#dvDistrictListTitle").find("span.sum").text(arr.length);
        map.clearOverlays();
        var pArr = [];
        $.each(arr, function (i, v) {
            var p = new BMap.Point(parseFloat(v.currentLng), parseFloat(v.currentLat));
            pArr.push(p);
            var img = parkMSys.jsSiteHost + "/images/man.jpg";
            var myIcon = new BMap.Icon(img, new BMap.Size(12, 29),
                                      {
                                          offset: new BMap.Size(6, 29),
                                          imageOffset: new BMap.Size(0, 0)
                                      });
            var marker = new BMap.Marker(p, { icon: myIcon });
            map.addOverlay(marker);

            marker.addEventListener("click", function () {
                var html = $("#manInfoTmpl").tmpl(v);
                var opts = {
                    width: 320,
                    height: 300,
                    offset: new BMap.Size(0, 0),
                    title: v.title
                }
                var infoWindow = new BMap.InfoWindow(html[0].outerHTML, opts);
                marker.openInfoWindow(infoWindow);
            });
        });
        map.setViewport(pArr);
    });    
};

function searchInspenctionMen() {
    var key = $.trim($("#txtKey").val());
    if (key == "") {
        alert("查询前请输入关键字!");
        return;
    }
    ajax("searchInspenctionMen", { name: encodeURIComponent(key) }, function (arr) {
        $("#dvDistrictListTitle").show().find("span.key").text(key).end().find("span.sum").text(arr.length);
        $("#dvDistrictList").empty().append("#manListTmpl", arr);
        map.clearOverlays();
        var pArr = [];
        $.each(arr, function (i, v) {
            var p = new BMap.Point(parseFloat(v.currentLng), parseFloat(v.currentLat));
            pArr.push(p);
            var img = parkMSys.jsSiteHost + "/images/man.jpg";
            var myIcon = new BMap.Icon(img, new BMap.Size(12, 29),
                                      {
                                          offset: new BMap.Size(6, 29),
                                          imageOffset: new BMap.Size(0, 0)
                                      });
            var marker = new BMap.Marker(p, { icon: myIcon });
            map.addOverlay(marker);

            marker.addEventListener("click", function () {
                var html = $("#manInfoTmpl").tmpl(v);
                var opts = {
                    width: 320,
                    height: 300,
                    offset: new BMap.Size(0, 0),
                    title: v.title
                }
                var infoWindow = new BMap.InfoWindow(html[0].outerHTML, opts);
                marker.openInfoWindow(infoWindow);
            });
        });
        map.setViewport(pArr);
    });
};

function getRouteOfInspectionMan(id, bhistory,item) {
    if (!bhistory) {
        $(".districtItem>div").addClass("noclick");
        $(item).find("div:first").removeClass("noclick").addClass("click");
    }
    ajax("getRouteOfInspectionMan", { id: id }, function (obj) {
        if (bhistory) {
            obj.bHistory = true;
        }
        var bClick = false;
        map.clearOverlays();
        var pArr = [];
        $.each(obj.data, function (i, v) {
            var p = new BMap.Point(parseFloat(v.lng), parseFloat(v.lat));
            pArr.push(p);
            var img = parkMSys.jsSiteHost + "/images/man.jpg";
            var myIcon = new BMap.Icon(img, new BMap.Size(12, 29),
                                      {
                                          offset: new BMap.Size(6, 29),
                                          imageOffset: new BMap.Size(0, 0)
                                      });
            var marker = new BMap.Marker(p, { icon: myIcon });
            map.addOverlay(marker);


            marker.addEventListener("click", function () {
                bClick = true;
                var html = $("#manInfoTmpl").tmpl(obj);
                var opts = {
                    width: 320,
                    height: 300,
                    offset: new BMap.Size(0, 0),
                    title: v.title
                }
                var infoWindow = new BMap.InfoWindow(html[0].outerHTML, opts);
                marker.openInfoWindow(infoWindow);
            });
        });
        if (bhistory) {
            var polyline = new BMap.Polyline(pArr);
            var n = 0;
            map.addOverlay(polyline, { strokeColor: "blue", strokeWeight: 1, strokeOpacity: 0.5 });
            var img = parkMSys.jsSiteHost + "/images/wk.png";
            var myIcon = new BMap.Icon(img, new BMap.Size(103, 103),
                                      {
                                          offset: new BMap.Size(51, 103),
                                          imageOffset: new BMap.Size(0, 0)
                                      });
            var marker = new BMap.Marker(pArr[n], { icon: myIcon });
            map.addOverlay(marker);
            (function () {
                if (!bClick) {
                    var callee = arguments.callee;
                    n = ++n % pArr.length;
                    setTimeout(function () { marker.setPosition(pArr[n]); callee(); }, 1000);
                }
            })()
        }
        map.setViewport(pArr);
    });
};

function OpenDispatchInspectionMan(number) {
    var html = $("#dispatchInfoTmpl").tmpl({number:number});

    var content = html[0].outerHTML;
    var title = "调度巡检员";
    
    var InfoWnd = map.getInfoWindow();

    if (InfoWnd == null || !InfoWnd.isOpen()) {
        return;
    }

    InfoWnd.setWidth(400);
    InfoWnd.setHeight(400);
    InfoWnd.setContent(content);
    InfoWnd.setTitle(title);

    $("#txtTaskTime").datetimepicker({ dateFormat: "yy-mm-dd" });
    $("#txtTaskTime").datetimepicker("setDate", new Date());
    $("#btnSubmit").click(function () {
        var sn = $.trim($("#txtSn").val());
        var taskNumber = $.trim($("#txtTask").val());
        var managerId = $.trim($("#txtManager").val());
        var inspectionManId = $.trim($("#txtMan").val());
        var taskTime = $.trim($("#txtTaskTime").val());
        dispatchInspectionMan(sn, taskNumber, managerId, inspectionManId, taskTime);
    });
};

function dispatchInspectionMan(sn, taskNumber, managerId, inspectionManId, taskTime) {
    ajax("dispatchInspectionMan", { sn: sn, taskNumber: taskNumber, managerId: managerId, inspectionManId: inspectionManId, taskTime: taskTime }, function (obj) {
        alert(obj.msg);
        if (obj.success == 1) {

        }
        else {

        }
    });
};